<?php
// +----------------------------------------------------------------------
// | quanli team
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2020 http://www.fistforward.com/ All rights reserved.
// +----------------------------------------------------------------------
// | Author: allen.qiang
// +----------------------------------------------------------------------

/**
 * 会员等级类
 * @package  module/base/customer
 * @author   allen.qiang
 * @version 1.0
 */

class custRank {

    /**
     * 查询会员等级信息
     * @param array $arg_array
     * $arg_array = array (
     *       'sortname' => string 排序字段
     *       'sortorder' => string 排序方式
     *       'page' => int 分页页码
     *       'rp' => int 分页页数
     * )
     * @return null
     */
    public function searchRank ( &$arg_array ) {
        
        global $G_SHOP;

        //sql字符串
        $sql = '';

        //参数类型字符串
        $params_type = '';

        //参数值数组
        $params_array = array();


        //会员等级号
        if (isset($arg_array['rankID']) && is_numeric($arg_array['rankID'])) {
            $sql .= ' AND rankID = ?';
            $params_type .= 'i';
            $params_array[] = $arg_array['rankID'];
        }

        //会员等级名称
        if (isset($arg_array['rankName']) && !empty($arg_array['rankName'])) {
            $sql .= ' AND rankName LIKE ?';
            $params_type .= 's';
            $params_array[] = '%' . $arg_array['rankName'] . '%';
        }

        //使用标志
        if (isset($arg_array['useFlg']) && in_array($arg_array['useFlg'], array(0, 1))) {
            $sql .= ' AND useFlg = ?';
            $params_type .= 'i';
            $params_array[] = $arg_array['useFlg'];
        }

        if (isset($arg_array['extend']) && !empty($arg_array['extend'])) {
            $sql .= $arg_array['extend'];
        }

        if ($sql) {
            $sql = 'WHERE ' . substr($sql, 4);
        }

        try {

            if (isset($arg_array['rp']) && $arg_array['rp'] > 1) {

                //统计数量
                $search_sql = 'SELECT COUNT(*) AS item_count FROM cust_rank ' . $sql;
                $count_array = &$G_SHOP->DBCA->stmt_query($search_sql, $params_type, $params_array);
                $item_count = $count_array[0]->item_count;
                if ($item_count == 0) {
                    $arg_array['count'] = 0;
                    return null;
                }

                $arg_array['count'] = $item_count;

                //排序
                if (isset($arg_array['sortname']) && !empty($arg_array['sortname']))
                    $sql .= sprintf(' ORDER BY %s %s', $arg_array['sortname'], $arg_array['sortorder']);

                //分页
                if (!isset($arg_array['page']) || !is_numeric($arg_array['page']) || $arg_array['page'] <= 0)
                    $arg_array['page'] = 1;

                $sql .= sprintf(' LIMIT %d, %d', ($arg_array['page'] - 1) * $arg_array['rp'], $arg_array['rp']);

            } else {

                //排序
                if (isset($arg_array['sortname']) && !empty($arg_array['sortname']))
                    $sql .= sprintf(' ORDER BY %s %s', $arg_array['sortname'], $arg_array['sortorder']);

            }

            //查询分类信息
            $search_sql = 'SELECT * FROM cust_rank ' . $sql;
            $temp_item_array = &$G_SHOP->DBCA->stmt_query($search_sql, $params_type, $params_array);
            if (!$temp_item_array)
                return null;

            $item_array = array();
            foreach($temp_item_array as &$item) {
                $item_array[] = array(
                    'rankID' => $item->rankID,
                    'rankName' => $item->rankName,
                    'needAmt' => $item->needAmt,
                    'rate' => $item->rate,
                    'pointsRate' => $item->pointsRate,
                    'freightFree' => $item->freightFree,
                    'useFlg' => $item->useFlg
                );

            }

            return $item_array;

        } catch (Exception $e) {

            error_log($e->getMessage());
            return null;

        }

    }

    /**
     * 根据会员累计消费金额得到会员等级
     * @param float $needAmt 会员累计消费金额
     * @return array|null
     */
    public function getRankForNeedAmt( $needAmt ) {

        $args_array = array(
            'useFlg' => 1,
            'sortname' => 'needAmt',
            'sortorder' => 'ASC'
        );

        /*$rank_array = $G_SHOP->sessionCache_get('RANK_ARRAY', true);
        if (!$rank_array) {
            $rank_array = $this->searchRank( $args_array );
            if (!$rank_array) {
                return null;
            }
            $G_SHOP->sessionCache_set('RANK_ARRAY', $rank_array, 0, true);
        }*/
        $rank_array = $this->searchRank( $args_array );
        if (!$rank_array) {
            return null;
        }

        $return_rank_array = array();
        foreach($rank_array as &$rank) {

            if ($rank['needAmt'] <= $needAmt) {
                $return_rank_array = $rank;
            }

        }

        return $return_rank_array;

    }

    /**
     * 新增或修改会员等级
     * @param array $arg_array
     * @return array
     */
    public function editCustomerRank ( $arg_array ) {

        global $G_SHOP;

        if (!isset($arg_array['rankID'], $arg_array['rankName'], $arg_array['needAmt'], $arg_array['rate'], $arg_array['pointsRate'],
             $arg_array['freightFree'], $arg_array['operator']) || empty($arg_array['operator'])) {
            return array( 'code' => 400, 'message' => '参数错误' );
        }

        if (empty($arg_array['rankName'])) {
            return array( 'code' => 400, 'message' => '等级名称不能为空' );
        }

        if (!is_numeric($arg_array['needAmt']) || $arg_array['needAmt'] < 0) {
            return array( 'code' => 400, 'message' => '累计消费金额必须为大于等于0的整数' );
        }

        if (!is_numeric($arg_array['rate']) || $arg_array['rate'] <= 0 || $arg_array['rate'] > 1) {
            return array( 'code' => 400, 'message' => '会员折扣必须为大于0，小于等于1的数字' );
        }

        if (!is_numeric($arg_array['pointsRate']) || $arg_array['pointsRate'] <= 0) {
            return array( 'code' => 400, 'message' => '赠送积分倍数必须为大于0的数字' );
        }

        if (!in_array($arg_array['freightFree'], array(0, 1))) {
            return array( 'code' => 400, 'message' => '请选择是否免运费' );
        }

        if ($arg_array['rankID'] < 0) {
            $arg_array['rankID'] = 0;
        }

        try {

            $rank_obj = new entity('cust_rank');
            if ($arg_array['rankID'] > 0) {
                $rank_obj->rankID = $arg_array['rankID'];
                if (!$G_SHOP->DBCA->fetch($rank_obj)) {
                    throw new Exception('会员等级信息没有找到', 404);
                }
                $rank_obj->editor = $arg_array['operator'];
            } else {
                $rank_obj->creator = $arg_array['operator'];
                $rank_obj->useFlg = 1;
            }
            $rank_obj->rankName = $arg_array['rankName'];
            $rank_obj->needAmt = $arg_array['needAmt'];
            $rank_obj->rate = $arg_array['rate'];
            $rank_obj->pointsRate = $arg_array['pointsRate'];
            $rank_obj->freightFree = $arg_array['freightFree'];

            $G_SHOP->DBCA->save($rank_obj);

            return array( 'code' => 200, 'message' => '保存成功' );

        } catch (Exception $e) {

            if ($e->getCode() > 0) {
                return array( 'code' => $e->getCode(), 'message' => $e->getMessage() );
            } else {
                error_log($e->getMessage());
                return array( 'code' => 500, 'message' => '系统错误' );
            }

        }
    }

}